<template>
    <div>

        <el-breadcrumb separator-icon="ArrowRight" style="margin: 16px">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>缺寝申请</el-breadcrumb-item>
        </el-breadcrumb>

        <el-card style="margin: 15px; min-height: calc(100vh - 111px)">
            <div class="box">
                <div class="items">
                    <p style="margin-bottom: 50px;">申请表单</p>
                    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
                       
                        <el-form-item label="学号" prop="stuId">
                           <el-input v-model="form.stuId" style="width: 80%"></el-input>
                        </el-form-item>
                        <el-form-item label="姓名" prop="stuName">
                            <el-input v-model="form.stuName" style="width: 80%"></el-input>
                        </el-form-item>
                        <el-form-item label="房间号" prop="roomId">
                            <el-input v-model="form.roomId" style="width: 80%"></el-input>
                        </el-form-item>
                        <el-form-item label="申请原因" prop="absReason">
                            <el-input  type="textarea" :row="3" placeholder="请输入内容" v-model="form.absReason" style="width: 80%"></el-input>
                        </el-form-item>
                        <el-form-item label="申请日期" prop="absDate">
                                <el-date-picker
                                v-model="form.absDate"
                                type="date"
                                placeholder="选择日期">
                                </el-date-picker>
                        </el-form-item>

                        <el-form-item>
                            <el-button type="primary" @click="onSubmit">提交申请</el-button>
                            <el-button @click="reset">重置</el-button>
                        </el-form-item>
                    </el-form>

                </div>
                <div class="items">
                    <p>历史申请</p>
                    <el-table v-loading="loading" :data="tableData"  max-height="1000" style="width: 100%">
                        <el-table-column label="申请时间" prop="absDate" />
                        <el-table-column label="申请原因" prop="absReason"/>
                        <el-table-column label="审核状态" prop="absStatus" :formatter="parseStatus">
                            <template slot-scope="scope">
                                <el-tag v-if = "scope.row.absStatus==null" type="info" effect="dark">未审核</el-tag>
                                <el-tag v-else-if = "scope.row.absStatus" type="success" effect="dark">通过</el-tag>
                                <el-tag v-else-if = "!scope.row.absStatus" type="danger" effect="dark">未通过</el-tag>
                             </template>
                        </el-table-column>
                    </el-table>
                </div>
            </div>
            

        </el-card>
    </div>
 
 

</template>

<script>
import request from '@/utils/request';
export default {
    name:"applyabsentRoom",
    data(){

        return {
            status:"",
            loading:true,
            form:{
                stuId:null,
                stuName:null,
                roomId:null,
                absReason:"",
                absDate:null
            },
            tableData:[
                {
                   
                    absDate:null,
                    absReason:null,
                    absStatus:null
                }
            ],
            rules: {
                stuId: [
                    {required: true, message: "请输入学号", trigger: "blur"},
                ],
                stuName: [
                    {required: true, message: "请输入姓名", trigger: "blur"},
                ],
                roomId:[{
                    required: true, message: "请填写房间号", trigger: "blur"
                }],
                absReason:[{
                    required: true, message: "请填写原因", trigger: "blur"
                }],
                absDate:[{
                    required: true, message: "时间不能为空", trigger: "blur"
                }]
               
            },
            stuid:null,
        }
    },
    created(){
        //获取登录的学生学号
        this.stuid=JSON.parse(window.localStorage.getItem('user')).stuId;
        this.load();
        this.loading = true;
        setTimeout(() => {
            //设置延迟执行
            this.loading = false;
        }, 1000);
    },
    methods:{
        async load(){
            this.stuid = parseInt(this.stuid)
            request.get("/absent/findByStuId", {
                params:{
                    stuId:this.stuid
                }
            }).then(res=>{
                if(res.code==="1"){
                    this.tableData=res.data;
                    this.loading=false;
                }
            })
        },
        parseStatus(row) {
         if(row.absStatus == null){
           return "未审核"
         }
         else if(!row.absStatus){
            return '不通过'
         }
         else {
            return '通过'
         }
        },
        async onSubmit(){
            this.$refs.form.validate(vaild=>{
                if(vaild){
                    request.post("/absent/applyadd",this.form).then(res=>{
                        if(res.code==="1"){
                            this.$message({
                            type:'success',
                            message:'成功提交申请'
                            })
                            this.load();
                            this.form={};
                        }
                        else{
                            this.$message({
                            type:'error',
                            message:res.msg
                            })
                        }
                    })
                }

            })
        },
        reset(){
            this.form={}
        }
    }
}
</script>

<style scoped>
    .box {
        margin-top:10px;
        display: flex;
        justify-content: space-between;
        flex-wrap: wrap;
    }
    .items {
        border-radius: 10px;
        justify-content: space-between;
      
        flex:0 0 45%;
        text-align: center;
        line-height:30px;
        margin-left: calc(4% / 3);
        margin-bottom: calc(4% / 3);
      
    }
</style>